package com.pro.springbootshop.service.impl;

import com.pro.springbootshop.entity.AfterSales;
import com.pro.springbootshop.entityExtend.AfterSalesReturn;
import com.pro.springbootshop.mapper.AfterSalesMapper;
import com.pro.springbootshop.service.AfterSalesService;
import com.pro.springbootshop.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AfterSalesServiceImpl implements AfterSalesService {
    @Autowired
    TokenUtil tokenUtil;
    public AfterSalesMapper afterSalesMapper;
    @Autowired
    public void setUserMapper (AfterSalesMapper afterSalesMapper) {
        this.afterSalesMapper = afterSalesMapper;
    }
    @Override
    public String saveAfterSales(AfterSales afterSales, String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        afterSales.setBuyerId(buyerId);
        afterSales.setAfterSalesState(0);
        int i=afterSalesMapper.insert(afterSales);
        if(i>0){
            return "提交成功";
        }else {
            return "提交失败";
        }
    }

    @Override
    public AfterSalesReturn getAllAfterSales(Integer page, Integer limit) {
        int pageNo=(page-1)*limit;
        int total=afterSalesMapper.selectCount(null);
        List<AfterSales> afterSalesList=afterSalesMapper.getAllAfterSales(pageNo,limit);
        for(AfterSales afterSales:afterSalesList){
            afterSales.setPictureList(afterSales.getAfterSalesPictures().split(";"));
        }
        return new AfterSalesReturn(afterSalesList,total);
    }

    @Override
    public AfterSalesReturn buyerGetAllAfterSales(Integer page, Integer limit, String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        int pageNo=(page-1)*limit;
        int total=afterSalesMapper.buyerGetAllAfterSalesCounts(buyerId).size();
        List<AfterSales> afterSalesList=afterSalesMapper.buyerGetAllAfterSales(pageNo,limit,buyerId);
        for(AfterSales afterSales:afterSalesList){
            afterSales.setPictureList(afterSales.getAfterSalesPictures().split(";"));
        }
        return new AfterSalesReturn(afterSalesList,total);
    }

    @Override
    public String changeAfterSales(Integer afterSalesId, Integer afterSalesState) {
        AfterSales afterSales=afterSalesMapper.selectById(afterSalesId);
        afterSales.setAfterSalesState(afterSalesState);
        int i=afterSalesMapper.updateById(afterSales);
        if(i>0){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }
}
